from odoo import fields, models, api
from odoo.exceptions import UserError


class RPTranslationTable(models.Model):
    _name = 'rp.translation.table'
    _description = "翻译对照表"
    _rec_name = "code"
    _order = 'create_datetime desc'
    _sql_constraints = [('unique_code', 'UNIQUE(code)', '编码不能重复')]

    code = fields.Char(string='编码', default='TRAN******', index=True, copy=False, readonly=True)
    create_datetime = fields.Datetime(string='创建时间', default=fields.Datetime.now, copy=False, readonly=True)

    program_id = fields.Many2one(string='项目', comodel_name='rp.translation.program')
    client_id = fields.Many2one(string='终端', comodel_name='rp.translation.client')
    index_id = fields.Many2one(string='索引', comodel_name='rp.translation.index')

    chinese = fields.Char(string='简体中文', index=True, copy=False)
    english = fields.Char(string='英文(US)', index=True, copy=False)

    spanish = fields.Char(string='西语(Español)')
    catalan = fields.Char(string='加泰罗尼亚语(Català)')
    french = fields.Char(string='法语(French)')

    active = fields.Boolean(string='有效', default=True)

    @api.model_create_multi
    def create(self, vals):
        for val in vals:
            val['code'] = self.env['ir.sequence'].sudo().next_by_code('rp.translation.table') or '/'
        return super(RPTranslationTable, self).create(vals)
